// index.js
// 获取应用实例
const app = getApp()
let typingTime = null
import { postGpt } from '../../service/gpt'
// 打字机声音
// let AudioContextoll = wx.createInnerAudioContext({ useWebAudioImplement: true })
Page({
  data: {
    strings: '',
    noviceText: '',
    writeContent: ''
  },
  onLoad() {
    let string = '11月1日星期天晴昨天是万圣节，我和吉xx一起去周xx家玩。我们带上吉xx妈咪买的面具，到了周xx家，周xx一下就认出我们俩了。吃完饭，我们一起弹钢琴，把两个声部的歌变成'
    this.setData({
          strings: string
    })
    //AudioContextoll.src = "https://oss-test.newplay7.com/20221229/16723073755532453.mp3"
    //AudioContextoll.play()
    typingTime && clearTimeout(typingTime)
    typingTime = setTimeout(() => {
      this.textTyping(string, 0)
    }, 500)
  },
  textTyping(s, x) {
    if (s.length <= x) {
      //AudioContextoll.stop()
      return
    }
    typingTime = setTimeout(() => {
      this.setData({
        noviceText: this.data.noviceText + s[x]
      })
      this.textTyping(s, x + 1)
    }, 50)
},
//点击结束打字效果
popUpClick() {
    this.setData({
      noviceText: this.data.strings
    })
    //AudioContextoll.stop()
    clearTimeout(typingTime)
},
changeContent(e) {
  this.setData({
    writeContent: e.detail.value
  })
},
sendBtn() {
  postGpt({prompt: this.data.writeContent}).then((res) => {
    console.log(res)
  })
}
})
